//
//  NSString+Common.m
//  Coffee
//
//  Created by 周天雨 on 2017/8/17.
//  Copyright © 2017年 周天雨. All rights reserved.
//

#import "NSString+Common.h"

@implementation NSString (Common)

/*!
 * @brief 把格式化的JSON格式的字符串转换成字典
 * @param jsonString JSON格式的字符串
 * @return 返回字典
 */
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

- (CGFloat)getLabSizeWithFont:(CGFloat )font AndSize:(CGFloat)sizeWidth{
    CGRect rect = [self boundingRectWithSize:CGSizeMake((sizeWidth - 70)*2*[UIScreen mainScreen].bounds.size.width/640, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    return rect.size.height+15;
}


- (CGSize)gettextSizeWithFont:(CGFloat)font AndSize:(CGFloat)sizeWidth {
    CGRect rect = [self boundingRectWithSize:CGSizeMake(sizeWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    return rect.size;
    
}


+ (BOOL)stringContainsEmoji:(NSString *)string
{
    __block BOOL returnValue = NO;
    
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                const unichar hs = [substring characterAtIndex:0];
                                if (0xd800 <= hs && hs <= 0xdbff) {
                                    if (substring.length > 1) {
                                        const unichar ls = [substring characterAtIndex:1];
                                        const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                                        if (0x1d000 <= uc && uc <= 0x1f77f) {
                                            returnValue = YES;
                                        }
                                    }
                                } else if (substring.length > 1) {
                                    const unichar ls = [substring characterAtIndex:1];
                                    
                                    if (ls == 0x20e3) {
                                        returnValue = YES;
                                    }
                                } else {
                                    if (0x2100 <= hs && hs <= 0x27ff) {
                                        returnValue = YES;
                                    } else if (0x2B05 <= hs && hs <= 0x2b07) {
                                        returnValue = YES;
                                    } else if (0x2934 <= hs && hs <= 0x2935) {
                                        returnValue = YES;
                                    } else if (0x3297 <= hs && hs <= 0x3299) {
                                        returnValue = YES;
                                    } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
                                        returnValue = YES;
                                    }
                                }
                            }];
    
    return returnValue;
}


+ (NSString *)getTimeString:(NSInteger)day {
    //设置日期格式
    NSDate *nowDate = [NSDate date];
    NSTimeInterval oneDay =24*60*60*1;//1天的长度
    NSDate *theDate = [nowDate initWithTimeIntervalSinceNow:+oneDay*day];
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    
    [formatter setLocale:[NSLocale currentLocale]];
    
    [formatter setDateFormat:@"yyyyMMdd"];
    
    //日期格式转成字符串
    
    NSString * date1 = [formatter stringFromDate:theDate];
    return date1;
}

+(NSString *)getstringtimeString:(NSString *)string {
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[NSLocale currentLocale]];;
    [inputFormatter setDateFormat:@"yyyyMMddHHmm"];
    NSDate* inputDate = [inputFormatter dateFromString:string];
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[NSLocale currentLocale]];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSString * date1 = [formatter stringFromDate:inputDate];
    return date1;
}

+ (NSString *)getstringTime:(NSString *)string {
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[NSLocale currentLocale]];;
    [inputFormatter setDateFormat:@"yyyyMMddHHmm"];
    NSDate* inputDate = [inputFormatter dateFromString:string];
    
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setLocale:[NSLocale currentLocale]];
    [outputFormatter setDateFormat:@"MM月dd日 HH:mm"];
    NSString *str = [outputFormatter stringFromDate:inputDate];
    return str;
}

+ (NSString*)getdayString:(NSString *)string {
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[NSLocale currentLocale]];;
    [inputFormatter setDateFormat:@"yyyyMMddHHmm"];
    NSDate* inputDate = [inputFormatter dateFromString:string];
    NSTimeZone *zone1 = [NSTimeZone systemTimeZone];
    NSInteger interval1 = [zone1 secondsFromGMTForDate: inputDate];
    NSDate *inputDate1 = [inputDate  dateByAddingTimeInterval: interval1];
    
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setLocale:[NSLocale currentLocale]];
    [outputFormatter setDateFormat:@"MM月dd日"];
    NSString *str1 = [outputFormatter stringFromDate:inputDate];
    
    NSDateFormatter *outputFormatter2 = [[NSDateFormatter alloc] init];
    [outputFormatter2 setLocale:[NSLocale currentLocale]];
    [outputFormatter2 setDateFormat:@"HH:mm"];
    NSString *str2 = [outputFormatter2 stringFromDate:inputDate];
    
    NSTimeInterval secondsPerDay = 24 * 60 * 60;
    NSDate *today = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: today];
    NSDate *localeDate = [today  dateByAddingTimeInterval: interval];
    NSDate *tomorrow, *yesterday, *thirdDay;
    
    tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
    yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
    thirdDay = [tomorrow dateByAddingTimeInterval:secondsPerDay];
    
    // 10 first characters of description is the calendar date:
    NSString * todayString = [[localeDate description] substringToIndex:10];
    NSString * yesterdayString = [[yesterday description] substringToIndex:10];
    NSString * tomorrowString = [[tomorrow description] substringToIndex:10];
    NSString * thiedString = [[thirdDay description] substringToIndex:10];
    
    NSString * dateString = [[inputDate1 description] substringToIndex:10];
    
    if ([dateString isEqualToString:todayString]) {
        dateString = @"今天";
    } else if ([dateString isEqualToString:yesterdayString]) {
        dateString = @"昨天";
    }else if ([dateString isEqualToString:tomorrowString]) {
        dateString = @"明天";
    } else if ([dateString isEqualToString:thiedString]) {
        dateString = @"后天";
    } else {
        dateString = @"";
    }
    return [NSString stringWithFormat:@"%@ %@ %@",str1, dateString, str2];
}

+ (NSString *)getTime:(NSString *)time {
    
    NSTimeInterval interval = [time floatValue]/1000;
    
    //时间戳转成date
    
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:interval];
    
    //设置日期格式
    
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    
    [formatter setLocale:[NSLocale currentLocale]];
    
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    
    //日期格式转成字符串
    
    NSString * date1 = [formatter stringFromDate:date];
    
    return date1;
}

+ (NSString *)getYYMMDD:(NSDate *)date {
    //设置日期格式
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[NSLocale currentLocale]];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    //日期格式转成字符串
    NSString * date1 = [formatter stringFromDate:date];
    return date1;
}

+ (NSString *)minutesTurnHour:(NSString *)minutes {
    CGFloat num = [minutes floatValue];
    NSString *time = nil;
    if (num < 60) {
        time = [NSString stringWithFormat:@"%.0f分",num];
    } else {
        time = [NSString stringWithFormat:@"%.1f小时",num/60];
    }
    return time;
}

+ (NSString *)distanceString:(NSString *)string {
    CGFloat num = [string floatValue];
    NSString *fansNum = nil;
    if (num < 1000) {
        fansNum = [NSString stringWithFormat:@"%.0f米",num];
    } else {
        fansNum = [NSString stringWithFormat:@"%.1f千米",num/1000];
    }
    return fansNum;
}

+ (NSString *)getTimeStringFromString:(NSString * )time {
    //    NSDate * d = [NSDate date];
    //
    //    NSTimeInterval late = [d timeIntervalSince1970]*1;
    //
    NSString * timeStr = nil;
    
    NSDate * dat = [NSDate dateWithTimeIntervalSinceNow:0];
    
    NSTimeInterval now = [dat timeIntervalSince1970]*1;
    
    NSTimeInterval cha = now - [time doubleValue]/1000;
    if (cha/3600 < 1) {
        
        timeStr = [NSString stringWithFormat:@"%f", cha/60];
        
        timeStr = [timeStr substringToIndex:timeStr.length-7];
        
        int num= [timeStr intValue];
        
        if (num <= 1) {
            
            timeStr = [NSString stringWithFormat:@"刚刚..."];
            
        }else{
            
            timeStr = [NSString stringWithFormat:@"%@分钟前", timeStr];
            
        }
        
    }
    
    if (cha/3600 > 1 && cha/86400 < 1) {
        
        timeStr = [NSString stringWithFormat:@"%f", cha/3600];
        
        timeStr = [timeStr substringToIndex:timeStr.length-7];
        
        timeStr = [NSString stringWithFormat:@"%@小时前", timeStr];
        
    }
    
    if (cha/86400 > 1)
        
    {
        
        timeStr = [NSString stringWithFormat:@"%f", cha/86400];
        
        timeStr = [timeStr substringToIndex:timeStr.length-7];
        
        int num = [timeStr intValue];
        
        if (num < 2) {
            
            timeStr = [NSString stringWithFormat:@"昨天"];
            
        }else if(num == 2){
            
            timeStr = [NSString stringWithFormat:@"前天"];
            
        }else if (num > 2 && num <7){
            
            timeStr = [NSString stringWithFormat:@"%@天前", timeStr];
            
        }else if (num >= 7 && num <= 10) {
            
            timeStr = [NSString stringWithFormat:@"1周前"];
            
        }else if(num > 10){
            
            timeStr = [NSString stringWithFormat:@"%d天前",num];
            
        }
        
    }
    
    return timeStr;
}

+ (NSString *)timeFormattedFromeTimeInterval:(NSInteger)time {
    NSInteger hour = 1;
    NSInteger minutes = 1;
    NSInteger seconds = 1;
    if (time / 3600 < 0) {
        hour = time/3600 + 24;
    }else {
        hour = time / 3600;
    }
    if ((time / 60)%60 < 0) {
        minutes = (time / 60) % 60 + 60;
        if (time / 3600 <= 0 ) {
            hour = time / 3600 + 23;
        }
    } else {
        minutes = (time / 60) % 60;
    }
    seconds = time % 60;
    
    return [NSString stringWithFormat:@"%.2ld:%.2ld:%.2ld",(long)hour,minutes,seconds];
}

+ (NSString *)getCurrentTimeString {
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
    return [NSString stringWithFormat:@"%.0f",interval];
}

+ (BOOL)isBlankString:(NSString *)string {
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isEqual:[NSNull null]]) {
        return  YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

+ (NSString *)escapedString: (NSString *)str{
   
    NSString *charactersToEscape = @"!*'();:@&=+$,/?%#[]";
    NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
    NSString *encodedStr = [[NSString stringWithFormat:@"%@",str] stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
    return encodedStr;
    
}

+ (NSInteger)chineseCountOfString:(NSString *)string{
    
    int ChineseCount = 0;
    
    if (string.length == 0) {
        return 0;
    }
    
    for (int i = 0; i<string.length; i++) {
        
        unichar c = [string characterAtIndex:i];
        
        if (c >=0x4E00 && c <=0x9FA5)        {
            
            ChineseCount += 2;//汉字
            
        }
        else{
            ChineseCount += 1;
        }
        
    }
    
    return ChineseCount;
    
}
+ (NSString *)captureCharacter:(NSString *)string count:(NSInteger)count{
    NSString *str = [NSString stringWithFormat:@""];
    int ChineseCount = 0;
    
    for (int i = 0; i<string.length; i++) {
        
        if (ChineseCount > count) {
            
            return str;
            
        }else{
            
            unichar c = [string characterAtIndex:i];
            
            if (c >=0x4E00 && c <=0x9FA5){
                
                ChineseCount += 2;//汉字
                
            }
            else{
                ChineseCount += 1;
                
            }
            str = [string substringToIndex:i];
            
        }
        
        
    }
    return str;
    
}

// 把手机号第4-7位变成星号
+(NSString *)phoneNumToAsterisk:(NSString*)phoneNum{
    
    if (phoneNum.length>=7) {
        return [phoneNum stringByReplacingCharactersInRange:NSMakeRange(3,4) withString:@"****"];
    }
    return phoneNum;
}
// 把身份证生日部分位变成星号
+ (NSString *)replaceStringWithAsterisk:(NSString *)idCard withStartLoca:(NSInteger)startLocation length:(NSInteger)length {
    NSString *replaceStr = idCard;
    for (NSInteger i = 0; i < length; i++) {
        NSRange range = NSMakeRange(startLocation, 1);
        replaceStr = [replaceStr stringByReplacingCharactersInRange:range withString:@"*"];
        startLocation ++;
    }
    return replaceStr;
}

//token解析
+ (id)jwtDecodeWithJwtString:(NSString *)jwtStr {
    
    NSArray * segments = [jwtStr componentsSeparatedByString:@"."];
    NSString * base64String = [segments objectAtIndex:1];
    
    int requiredLength = (int)(4 *ceil((float)[base64String length]/4.0));
    int nbrPaddings = requiredLength - (int)[base64String length];
    if(nbrPaddings > 0) {
        NSString * pading = [[NSString string] stringByPaddingToLength:nbrPaddings withString:@"=" startingAtIndex:0];
        base64String = [base64String stringByAppendingString:pading];
    }
    base64String = [base64String stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
    base64String = [base64String stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
    NSData * decodeData = [[NSData alloc] initWithBase64EncodedData:[base64String dataUsingEncoding:NSUTF8StringEncoding] options:0];
    NSString * decodeString = [[NSString alloc] initWithData:decodeData encoding:NSUTF8StringEncoding];
    NSDictionary * jsonDict = [NSJSONSerialization JSONObjectWithData:[decodeString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
    return jsonDict;
}
//根据URL生成二维码
+ (UIImage *)df_createQRcodeWithUrlstring:(NSString *)urlString witImageSize:(CGSize)size{
       
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSData *data = [urlString dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKey:@"inputMessage"];
    CIImage *qrImage = [filter outputImage];
    //绘制
    CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
    UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(cgImage);
    return codeImage;
}
//根据内容生成二维码
+ (UIImage *)df_createQRcodeWithContent:(NSString *)title withIdCard:(NSString *)idCard withCardNum:(NSString *)cardNo withMobile:(NSString *)mobile witImageSize:(CGSize)size{
       
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSString *dataStr = [NSString stringWithFormat:@"%@ %@ %@ %@",title,idCard,cardNo,mobile];
    NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKey:@"inputMessage"];
    CIImage *qrImage = [filter outputImage];
    //绘制
    CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
    UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRelease(cgImage);
    return codeImage;
}
//身份证号码校验
- (BOOL)validateIdentityCard {
    BOOL flag;
    if (self.length <= 0) {
        
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:self];
}
//密码强度判断:0弱 1一般 2强
+ (BOOL)judgeRange:(NSArray*)conditionArr Password:(NSString*)password{
    
    NSRange range;
    BOOL result =NO;
    for(int i=0; i<[conditionArr count]; i++){
        
        range = [password rangeOfString:[conditionArr objectAtIndex:i]];
        if(range.location != NSNotFound){
            
            result =YES;
        }
    }
    return result;
}
//否一个HttpURL
+ (BOOL)isHttpUrl:(NSString *)url{
    BOOL isHttp = NO;
    if ([url containsString:@"http:"] || [url containsString:@"https:"]) {
        isHttp = YES;
    }
    return isHttp;
}

//手机设备判断
+ (NSString *)machineModel {
    static dispatch_once_t one;
    static NSString *model;
    dispatch_once(&one, ^{
        size_t size;
        sysctlbyname("hw.machine", NULL, &size, NULL, 0);
        char *machine = malloc(size);
        sysctlbyname("hw.machine", machine, &size, NULL, 0);
        model = [NSString stringWithUTF8String:machine];
        free(machine);
    });
    return model;
}

+ (NSString *)machineModelName {
    static dispatch_once_t one;
    static NSString *name;
    dispatch_once(&one, ^{
        NSString *model = [self machineModel];
        if (!model) return;
        NSDictionary *dic = @{
            @"Watch1,1" : @"Apple Watch 38mm",
            @"Watch1,2" : @"Apple Watch 42mm",
            @"Watch2,3" : @"Apple Watch Series 2 38mm",
            @"Watch2,4" : @"Apple Watch Series 2 42mm",
            @"Watch2,6" : @"Apple Watch Series 1 38mm",
            @"Watch1,7" : @"Apple Watch Series 1 42mm",
            
            @"iPod1,1" : @"iPod touch 1",
            @"iPod2,1" : @"iPod touch 2",
            @"iPod3,1" : @"iPod touch 3",
            @"iPod4,1" : @"iPod touch 4",
            @"iPod5,1" : @"iPod touch 5",
            @"iPod7,1" : @"iPod touch 6",
            
            @"iPhone1,1" : @"iPhone 1G",
            @"iPhone1,2" : @"iPhone 3G",
            @"iPhone2,1" : @"iPhone 3GS",
            @"iPhone3,1" : @"iPhone 4 (GSM)",
            @"iPhone3,2" : @"iPhone 4",
            @"iPhone3,3" : @"iPhone 4 (CDMA)",
            @"iPhone4,1" : @"iPhone 4S",
            @"iPhone5,1" : @"iPhone 5",
            @"iPhone5,2" : @"iPhone 5",
            @"iPhone5,3" : @"iPhone 5c",
            @"iPhone5,4" : @"iPhone 5c",
            @"iPhone6,1" : @"iPhone 5s",
            @"iPhone6,2" : @"iPhone 5s",
            @"iPhone7,1" : @"iPhone 6 Plus",
            @"iPhone7,2" : @"iPhone 6",
            @"iPhone8,1" : @"iPhone 6s",
            @"iPhone8,2" : @"iPhone 6s Plus",
            @"iPhone8,4" : @"iPhone SE",
            @"iPhone9,1" : @"iPhone 7",
            @"iPhone9,2" : @"iPhone 7 Plus",
            @"iPhone9,3" : @"iPhone 7",
            @"iPhone9,4" : @"iPhone 7 Plus",
            @"iPhone10,1": @"iPhone 8",
            @"iPhone10,4": @"iPhone 8",
            @"iPhone10,2": @"iPhone 8 Plus",
            @"iPhone10,5": @"iPhone 8 Plus",
            @"iPhone10,3": @"iPhone X",
            @"iPhone10,6": @"iPhone X",
            @"iPhone11,8": @"iPhone XR",
            @"iPhone11,2": @"iPhone XS",
            @"iPhone11,6": @"iPhone XS Max",
            @"iPhone12,1": @"iPhone 11",
            @"iPhone12,3": @"iPhone 11 Pro",
            @"iPhone12,5": @"iPhone 11 Pro Max",
            @"iPhone12,8": @"iPhone SE (2nd generation)",
            @"iPhone13,1": @"iPhone 12 mini",
            @"iPhone13,2": @"iPhone 12",
            @"iPhone13,3": @"iPhone 12 Pro",
            @"iPhone13,4": @"iPhone 12 Pro Max",
            @"iPhone14,4": @"iPhone 13 mini",
            @"iPhone14,5": @"iPhone 13",
            @"iPhone14,2": @"iPhone 13 Pro",
            @"iPhone14,3": @"iPhone 13 Pro Max",
            
            
            @"iPad1,1" : @"iPad 1",
            @"iPad2,1" : @"iPad 2 (WiFi)",
            @"iPad2,2" : @"iPad 2 (GSM)",
            @"iPad2,3" : @"iPad 2 (CDMA)",
            @"iPad2,4" : @"iPad 2",
            @"iPad2,5" : @"iPad mini 1",
            @"iPad2,6" : @"iPad mini 1",
            @"iPad2,7" : @"iPad mini 1",
            @"iPad3,1" : @"iPad 3 (WiFi)",
            @"iPad3,2" : @"iPad 3 (4G)",
            @"iPad3,3" : @"iPad 3 (4G)",
            @"iPad3,4" : @"iPad 4",
            @"iPad3,5" : @"iPad 4",
            @"iPad3,6" : @"iPad 4",
            @"iPad4,1" : @"iPad Air",
            @"iPad4,2" : @"iPad Air",
            @"iPad4,3" : @"iPad Air",
            @"iPad4,4" : @"iPad mini 2",
            @"iPad4,5" : @"iPad mini 2",
            @"iPad4,6" : @"iPad mini 2",
            @"iPad4,7" : @"iPad mini 3",
            @"iPad4,8" : @"iPad mini 3",
            @"iPad4,9" : @"iPad mini 3",
            @"iPad5,1" : @"iPad mini 4",
            @"iPad5,2" : @"iPad mini 4",
            @"iPad5,3" : @"iPad Air 2",
            @"iPad5,4" : @"iPad Air 2",
            @"iPad6,3" : @"iPad Pro (9.7 inch)",
            @"iPad6,4" : @"iPad Pro (9.7 inch)",
            @"iPad6,7" : @"iPad Pro (12.9 inch)",
            @"iPad6,8" : @"iPad Pro (12.9 inch)",
            
            @"AppleTV2,1" : @"Apple TV 2",
            @"AppleTV3,1" : @"Apple TV 3",
            @"AppleTV3,2" : @"Apple TV 3",
            @"AppleTV5,3" : @"Apple TV 4",
            
            @"i386" : @"Simulator x86",
            @"x86_64" : @"Simulator x64",
        };
        name = dic[model];
        if (!name) name = model;
    });
    return name;
}
///拨打电话
+ (void)callMobile:(NSString *)phone{
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",phone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
///通过URL打开浏览器
+ (void)applicationOpenUrl:(NSString *)params{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:params]];
}
///检测URL是否能够ping通
+ (void)validateUrl:(NSString *)candidate callBack:(void(^)(BOOL))callback {
    NSLog(@"host验证:%@",candidate);
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:candidate]];
    [request setHTTPMethod:@"HEAD"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (!request || error) {
            callback(NO);
        }else{
            callback(YES);
        }
    }];
    [task resume];
}
+ (BOOL)isEmpty:(NSString *)string
{
    if (!string) {
        return true;
    } else {
        
        if ([string isKindOfClass:[NSString class]]) {
            if (string == nil || [string isKindOfClass:[NSNull class]] || (string.length == 0) || [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [string isEqualToString:@"<null>"] || [string isEqualToString:@"null"]) {
                
                return true;
            } else {
                return false;
            }
        }else if ([string isKindOfClass:[NSNumber class]]){
            NSNumber *num = (NSNumber *)string;
            NSString *str = [num stringValue];
            if (str == nil || [str isKindOfClass:[NSNull class]] || (str.length == 0) || [[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [str isEqualToString:@"<null>"] || [str isEqualToString:@"null"]) {
                
                return true;
            } else {
                return false;
            }
        }else if ([string isEqual:[NSNull null]]){
            
            return YES;
        }
        
    }
    return  false;
    
}

+ (NSString *)convertStringWithTime:(float)time {
    if (isnan(time)) time = 0.f;
    int min = time / 60.0;
    int sec = time - min * 60;
    NSString * minStr = min > 9 ? [NSString stringWithFormat:@"%d",min] : [NSString stringWithFormat:@"0%d",min];
    NSString * secStr = sec > 9 ? [NSString stringWithFormat:@"%d",sec] : [NSString stringWithFormat:@"0%d",sec];
    NSString * timeStr = [NSString stringWithFormat:@"%@:%@",minStr, secStr];
    return timeStr;
}
@end

